package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.INetworkSalesVolumeService;
import com.mdd.admin.validate.NetworkSalesVolumeCreateValidate;
import com.mdd.admin.validate.NetworkSalesVolumeUpdateValidate;
import com.mdd.admin.validate.NetworkSalesVolumeSearchValidate;
import com.mdd.admin.vo.NetworkSalesVolumeListedVo;
import com.mdd.admin.vo.NetworkSalesVolumeDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.NetworkSalesVolume;
import com.mdd.common.export.NetworkSalesVolumeListedExport;
import com.mdd.common.mapper.NetworkSalesVolumeMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 网销客户实现类
 * @author LikeAdmin
 */
@Service
public class NetworkSalesVolumeServiceImpl implements INetworkSalesVolumeService {
        
    @Resource
    NetworkSalesVolumeMapper networkSalesVolumeMapper;

    /**
     * 网销客户列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<NetworkSalesVolumeListedVo>
     */
    @Override
    public PageResult<NetworkSalesVolumeListedVo> list(PageValidate pageValidate, NetworkSalesVolumeSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<NetworkSalesVolume> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        networkSalesVolumeMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:clientName@client_name:str",
            "=:state:int",
            "=:clientSource@client_source:str",
            "=:belongIndustry@belong_industry:str",
            "=:advisoryService@advisory_service:str",
            "=:businessPrice@business_price:str",
            "=:contacts:str",
            "=:post:str",
            "=:phone:str",
            "=:province:str",
            "=:city:str",
            "=:district:str",
            "=:salesman:str",
            "=:operateTime@operate_time:str",
        });

        IPage<NetworkSalesVolume> iPage = networkSalesVolumeMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<NetworkSalesVolumeListedVo> list = new LinkedList<>();
        for(NetworkSalesVolume item : iPage.getRecords()) {
            NetworkSalesVolumeListedVo vo = new NetworkSalesVolumeListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public List<NetworkSalesVolumeListedExport> list() {
        List<NetworkSalesVolume> networkSalesVolumes = networkSalesVolumeMapper.selectList(new QueryWrapper<>());
        ArrayList<NetworkSalesVolumeListedExport> networkSalesVolumeListedVos = new ArrayList<>();
        for (NetworkSalesVolume networkSalesVolume : networkSalesVolumes) {
            NetworkSalesVolumeListedExport networkSalesVolume1 = new NetworkSalesVolumeListedExport();
            BeanUtils.copyProperties(networkSalesVolume, networkSalesVolume1);
            if (networkSalesVolume.getState() == 0) {
                networkSalesVolume1.setState("未分配");
            }else if(networkSalesVolume.getState() == 1){
                networkSalesVolume1.setState("已分配");
            }else {
                networkSalesVolume1.setState("未编辑");
            }
            networkSalesVolumeListedVos.add(networkSalesVolume1);
        }
        return networkSalesVolumeListedVos;
    }

    /**
     * 网销客户详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return NetworkSalesVolume
     */
    @Override
    public NetworkSalesVolumeDetailVo detail(Integer id) {
        NetworkSalesVolume model = networkSalesVolumeMapper.selectOne(
                new QueryWrapper<NetworkSalesVolume>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        NetworkSalesVolumeDetailVo vo = new NetworkSalesVolumeDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 网销客户新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(NetworkSalesVolumeCreateValidate createValidate) {
        NetworkSalesVolume model = new NetworkSalesVolume();
        model.setClientName(createValidate.getClientName());
        model.setState(createValidate.getState());
        model.setClientSource(createValidate.getClientSource());
        model.setBelongIndustry(createValidate.getBelongIndustry());
        model.setAdvisoryService(createValidate.getAdvisoryService());
        model.setBusinessPrice(createValidate.getBusinessPrice());
        model.setContacts(createValidate.getContacts());
        model.setPost(createValidate.getPost());
        model.setPhone(createValidate.getPhone());
        model.setProvince(createValidate.getProvince());
        model.setCity(createValidate.getCity());
        model.setDistrict(createValidate.getDistrict());
        model.setSalesman(createValidate.getSalesman());
        model.setOperateTime(createValidate.getOperateTime());
        networkSalesVolumeMapper.insert(model);
    }

    /**
     * 网销客户编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(NetworkSalesVolumeUpdateValidate updateValidate) {
        NetworkSalesVolume model = networkSalesVolumeMapper.selectOne(
                new QueryWrapper<NetworkSalesVolume>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setClientName(updateValidate.getClientName());
        model.setState(updateValidate.getState());
        model.setClientSource(updateValidate.getClientSource());
        model.setBelongIndustry(updateValidate.getBelongIndustry());
        model.setAdvisoryService(updateValidate.getAdvisoryService());
        model.setBusinessPrice(updateValidate.getBusinessPrice());
        model.setContacts(updateValidate.getContacts());
        model.setPost(updateValidate.getPost());
        model.setPhone(updateValidate.getPhone());
        model.setProvince(updateValidate.getProvince());
        model.setCity(updateValidate.getCity());
        model.setDistrict(updateValidate.getDistrict());
        model.setSalesman(updateValidate.getSalesman());
        model.setOperateTime(updateValidate.getOperateTime());
        networkSalesVolumeMapper.updateById(model);
    }

    /**
     * 网销客户删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        NetworkSalesVolume model = networkSalesVolumeMapper.selectOne(
                new QueryWrapper<NetworkSalesVolume>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        networkSalesVolumeMapper.delete(new QueryWrapper<NetworkSalesVolume>().eq("id", id));
    }

}
